package com.hb.game.pk.ui.activity.mine.wallet;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hb.game.pk.R;
import com.hb.game.pk.adpter.PaidListAdpter;
import com.hb.game.pk.adpter.RechargeListAdpter;
import com.hb.game.pk.adpter.TXListAdpter;
import com.hb.game.pk.adpter.TransListAdpter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.PaidBean;
import com.hb.game.pk.beans.RechargeListBean;
import com.hb.game.pk.beans.TXBean;
import com.hb.game.pk.beans.TransBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecordActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    int type;
    int p = 1;
    List<RechargeListBean.DataBean.ListBean> rechargeList;
    private List<RechargeListBean.DataBean.ListBean> rechargeMore;
    List<TXBean.DataBean.ListBean> txList;
    List<TXBean.DataBean.ListBean> txMore;
    private List<TransBean.DataBean.ListBean> transMore;
    private List<TransBean.DataBean.ListBean> transList;
    private List<PaidBean.DataBean.ListBean> paidList;
    private List<PaidBean.DataBean.ListBean> paidMore;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }

    @Override
    protected int getRootViewId() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecordActivity.this.finish();
            }
        });
        String title = "";
        if (type == 1) {
            title = "充值记录";
        } else if (type == 2) {
            title = "转账记录";
        } else if (type == 4) {
            title = "余额明细";
        } else {
            title = "提现记录";
        }
        titleBar.setTitle(title);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        if (type == 1) {
            rechargelist(false);
        } else if (type == 2) {
            transferinfo(false);
        } else if (type == 4) {
            paidlist(false);
        } else {
            txlist(false);
        }
    }

    private void transferinfo(final boolean isMore) {
        Call<TransBean> rechargelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).transferinfo(p);
        rechargelist.enqueue(new Callback<TransBean>() {
            @Override
            public void onResponse(Call<TransBean> call, Response<TransBean> response) {
                TransBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (isMore) {
                    transMore = body.getData().getList();
                    loadMoreSuccess();
                    if (rechargeMore != null && rechargeMore.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    transList = body.getData().getList();
                    initAdapter();
                    p++;
                }
            }

            @Override
            public void onFailure(Call<TransBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
                if (isMore) {
                    loadMoreFail();
                }
            }
        });
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        if (type == 1) {
            rechargelist(true);
        } else if (type == 2) {
            transferinfo(true);
        } else if (type == 4) {
            paidlist(true);
        } else {
            txlist(true);
        }
    }

    private void paidlist(final boolean isMore) {
        Call<PaidBean> rechargelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).paidlist(p);
        rechargelist.enqueue(new Callback<PaidBean>() {
            @Override
            public void onResponse(Call<PaidBean> call, Response<PaidBean> response) {
                PaidBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (isMore) {
                    paidMore = body.getData().getList();
                    loadMoreSuccess();
                    if (paidMore != null && paidMore.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    paidList = body.getData().getList();
                    initAdapter();
                    p++;
                }
            }

            @Override
            public void onFailure(Call<PaidBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
                if (isMore) {
                    loadMoreFail();
                }
            }
        });
    }

    private void rechargelist(final boolean isMore) {
        Call<RechargeListBean> rechargelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).rechargelist(p);
        rechargelist.enqueue(new Callback<RechargeListBean>() {
            @Override
            public void onResponse(Call<RechargeListBean> call, Response<RechargeListBean> response) {
                RechargeListBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (isMore) {
                    rechargeMore = body.getData().getList();
                    loadMoreSuccess();
                    if (rechargeMore != null && rechargeMore.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    rechargeList = body.getData().getList();
                    initAdapter();
                    p++;
                }
            }

            @Override
            public void onFailure(Call<RechargeListBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
                if (isMore) {
                    loadMoreFail();
                }
            }
        });
    }

    @Override
    protected void setMoreData() {
        super.setMoreData();
        if (type == 1) {
            if (rechargeMore != null)
                mAdapter.addData(rechargeMore);
        } else if (type == 2) {
            if (transMore != null)
                mAdapter.addData(transMore);
        } else if (type == 4) {
            if (paidMore != null)
                mAdapter.addData(paidMore);
        }else {
            if (txMore != null)
                mAdapter.addData(txMore);
        }
    }

    private void txlist(final boolean isMore) {
        Call<TXBean> txlist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).txlist(p);
        txlist.enqueue(new Callback<TXBean>() {
            @Override
            public void onResponse(Call<TXBean> call, Response<TXBean> response) {
                TXBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (isMore) {
                    txMore = body.getData().getList();
                    loadMoreSuccess();
                    if (txMore != null && txMore.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    txList = body.getData().getList();
                    initAdapter();
                    p++;
                }
            }

            @Override
            public void onFailure(Call<TXBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (type == 1) {
            if (mAdapter == null) {
                rechargeAdapter();
            } else {
                mAdapter.setNewData(rechargeList);
            }
        } else if (type == 2) {
            if (mAdapter == null) {
                transAdapter();
            } else {
                mAdapter.setNewData(transList);
            }
        } else if (type == 4) {
            if (mAdapter == null) {
                paidAdapter();
            } else {
                mAdapter.setNewData(paidList);
            }
        }else {
            if (mAdapter == null) {
                txAdapter();
            } else {
                mAdapter.setNewData(txList);
            }
        }
        super.initAdapter();
    }

    private void paidAdapter() {
        mAdapter = new PaidListAdpter(R.layout.item_record_list);
        mAdapter.setNewData(rechargeList);
    }

    private void transAdapter() {
        mAdapter = new TransListAdpter(R.layout.item_record_list);
        mAdapter.setNewData(rechargeList);
    }

    private void rechargeAdapter() {
        mAdapter = new RechargeListAdpter(R.layout.item_record_list);
        mAdapter.setNewData(rechargeList);
    }

    private void txAdapter() {
        mAdapter = new TXListAdpter(R.layout.item_record_list);
        mAdapter.setNewData(txList);
    }
}
